from rest_framework import serializers
from bike.models import Staff, Violation
from bike.models import Plate
from bike.models import Car
from bike.models import Student
from bike.models import Teacher


class StaffSerl(serializers.ModelSerializer):
    updated = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = Staff
        exclude = ("created",)


class PlateSerl(serializers.ModelSerializer):
    updated = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
    status_text = serializers.SerializerMethodField()
    kind_text = serializers.SerializerMethodField()

    class Meta:
        model = Plate
        exclude = ("created",)

    def get_status_text(self, obj):
        return obj.get_status_display()

    def get_kind_text(self, obj):
        return obj.get_kind_display()


class CarSerl(serializers.ModelSerializer):
    plate = PlateSerl()
    updated = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
    kind_text = serializers.SerializerMethodField()

    class Meta:
        model = Car
        exclude = ("created",)

    def get_kind_text(self, obj):
        return obj.get_kind_display()


class StudentSerl(serializers.ModelSerializer):
    updated = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = Student
        exclude = ("created",)


class TeacherSerl(serializers.ModelSerializer):
    updated = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = Teacher
        exclude = ("created",)


# 违章记录的序列化器
class ViolationSerl(serializers.ModelSerializer):
    staff = StaffSerl()
    car = CarSerl()
    updated = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = Violation
        exclude = ("created",)


class ViolationListSerl(serializers.ModelSerializer):
    updated = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = Violation
        fields = ("id", "updated",)
